﻿using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class RolePermissionFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public RolePermissionFrm()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = (from r in db.Roles
                                        select new
                                        {
                                            RoleName = r.Name,
                                            RoleRemark = r.Remark,
                                            RoleId = r.Id
                                        }).ToList();
            var list = (from p in db.Permissions.ToList()
                        select new
                        {
                            PermissionName = p.Name,
                            Remark = p.Remark,
                            Id = p.Id
                        }).ToList();
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = list;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridViewRow row = dataGridView1.CurrentRow;
                if (dataGridView1.CurrentRow.Cells["RoleId"].Value != null && (int)dataGridView1.CurrentRow.Cells["RoleId"].Value > 0)
                {
                    var role = db.Roles.Find((int)dataGridView1.CurrentRow.Cells["RoleId"].Value);
                    if (role != null)// && !string.IsNullOrEmpty(role.PermissionStr))
                    {
                        var list = (role.PermissionStr + "").Split(',');
                        var roles = (from r in db.Permissions//.ToList()                                  
                                     select new
                                     {
                                         PermissionName = r.Name,
                                         Remark = r.Remark,
                                         HaveOrNot = (list).Contains(r.Id.ToString()),
                                         Id = r.Id
                                     }).ToList();
                        dataGridView2.AutoGenerateColumns = false;
                        dataGridView2.DataSource = roles;
                    }
                }

            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridViewRow row = dataGridView1.CurrentRow;
                if (dataGridView1.CurrentRow.Cells["RoleId"].Value != null && (int)dataGridView1.CurrentRow.Cells["RoleId"].Value > 0)
                {
                    var role = db.Roles.Find((int)dataGridView1.CurrentRow.Cells["RoleId"].Value);
                    if (role != null && !string.IsNullOrEmpty(role.PermissionStr))
                    {
                        var list = (role.PermissionStr + "").Split(',');
                        var roles = (from r in db.Permissions//.ToList()                                  
                                     select new
                                     {
                                         PermissionName = r.Name,
                                         Remark = r.Remark,
                                         HaveOrNot = (list).Contains(r.Id.ToString()),
                                         Id = r.Id
                                     }).ToList();
                        dataGridView2.AutoGenerateColumns = false;
                        dataGridView2.DataSource = roles;
                    }
                }

            }
        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = (from r in db.Roles
                                        select new
                                        {
                                            RoleName = r.Name,
                                            RoleRemark = r.Remark,
                                            RoleId = r.Id,
                                            //HaveOrNot=

                                        }).ToList();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            var dia = new RoleFrm().ShowDialog();
            refreshBtn_Click(null, null);
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridViewRow row = dataGridView1.CurrentRow;
                if (dataGridView1.CurrentRow.Cells["RoleId"].Value != null && (int)dataGridView1.CurrentRow.Cells["RoleId"].Value > 0)
                {

                    var role = db.Roles.Find((int)dataGridView1.CurrentRow.Cells["RoleId"].Value);
                    if (role != null)///&& !string.IsNullOrEmpty(role.PermissionStr))
                    {
                        var list = (role.PermissionStr + "").Split(',');

                        if (dataGridView2.CurrentRow.Cells["Id"].Value != null && (int)dataGridView2.CurrentRow.Cells["Id"].Value > 0)
                        {
                            var id = (int)dataGridView2.CurrentRow.Cells["Id"].Value;
                            var havePermission = dataGridView2.CurrentRow.Cells["HaveOrNot"].Value == null ? false : (bool)dataGridView2.CurrentRow.Cells["HaveOrNot"].Value;
                            if (havePermission)
                            {
                                role.PermissionStr = string.Join(",", list.Where(o => !o.Equals(id.ToString())).ToList());
                                db.SaveChanges();
                            }
                            else if (!havePermission)
                            {
                                var strList = list.ToList();
                                strList.Add(id.ToString());
                                role.PermissionStr = string.Join(",", strList);
                                db.SaveChanges();
                            }
                            //dataGridView2.CurrentRow.Cells["HaveOrNot"].Value = (!havePermission);//?"True":"False";
                            RefreshGD2();
                        }

                    }
                }

            }
        }
        private void RefreshGD2()
        {
            DataGridViewRow row = dataGridView1.CurrentRow;
            if (dataGridView1.CurrentRow.Cells["RoleId"].Value != null && (int)dataGridView1.CurrentRow.Cells["RoleId"].Value > 0)
            {
                var role = db.Roles.Find((int)dataGridView1.CurrentRow.Cells["RoleId"].Value);
                if (role != null && !string.IsNullOrEmpty(role.PermissionStr))
                {
                    var list = role.PermissionStr.Split(',');
                    var roles = (from r in db.Permissions//.ToList()                                  
                                 select new
                                 {
                                     PermissionName = r.Name,
                                     Remark = r.Remark,
                                     HaveOrNot = (list).Contains(r.Id.ToString()),
                                     Id = r.Id
                                 }).ToList();
                    dataGridView2.AutoGenerateColumns = false;
                    dataGridView2.DataSource = roles;
                }
            }
        }

        private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }
    }
}
